﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Newtonsoft.Json;
using Ccit.OA.MsSystemDoc;
using System.Collections.Generic;
using Ccit.OA.Utils;
using Ccit.Utils;
namespace Ccit.OA.SystemDoc
{
    public partial class SystemDoc_codeRule_edit : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                this._hidClassType.Value = (null != Request.QueryString["classType"]) ? Request.QueryString["classType"].ToString() : "";
                this._hidCodeId.Value = (null != Request.QueryString["CodeID"]) ? Request.QueryString["CodeID"].ToString() : "";
               
                DataBinde();
                if (_hidCodeId.Value != "")
                { InitPage(); }
                else
                {
                    if (_hidClassType.Value != "")
                    {
                        ddlClassType.SelectedValue = _hidClassType.Value;
                        ddlClassType.Enabled = false;
                    }
                }
            }
        }

        private void InitPage()
        {
            SystemDocBLL Bll = new SystemDocBLL();
            MsCodeRule ms = Bll.GetCodeRule(_hidCodeId.Value);
            if(ms!=null)
            {
                ddlClassType.SelectedValue = ms.Primary;
                ddlClassType.Enabled = false;
                txtCodeName.Text = ms.CodeName;
                if(ms.CodeDefault=="1")
                { chkDefault.Checked = true; }
                List<MsCodeRuleDetail> lstDetail = Bll.GetCodeRuleDetailMes(_hidCodeId.Value);
                if(lstDetail!=null)
                {
                    for (int i = 0; i < lstDetail.Count; i++)
                    {
                        MsCodeRuleDetail msDetail = lstDetail[i];
                        if (msDetail.OneCodeNum != "")
                        {
                            ddlCode1.SelectedValue = (msDetail.OneCodeNum != "") ? msDetail.OneCodeNum : "";
                            if (msDetail.OneCodeNum == "0" || msDetail.OneCodeNum == "4" || msDetail.OneCodeNum == "5")
                            {
                                lblTypes.Attributes["style"] = "display:none";
                                txtNumber1.Attributes["style"] = "display:block";
                                lblNumber1.Attributes["style"] = "display:block";
                                if (msDetail.OneCodeNum == "0")
                                { 
                                    lblNumber1.Text = "值   ";
                                    txtNumber1.Text = msDetail.OneCons;
                                }
                                if (msDetail.OneCodeNum == "4" || msDetail.OneCodeNum == "5")
                                { 
                                    lblNumber1.Text = "位数 ";
                                    txtNumber1.Text = msDetail.OneMedian;
                                }
                            }
                            ddlOneSeparator.Text = (msDetail.OneSeparator != "") ? msDetail.OneSeparator : "";
                        }
                        if (msDetail.TwoCodeNum != "")
                        {
                            ddlCode2.SelectedValue = (msDetail.TwoCodeNum != "") ? msDetail.TwoCodeNum : "";
                            if (msDetail.TwoCodeNum == "0" || msDetail.TwoCodeNum == "4" || msDetail.TwoCodeNum == "5")
                            {
                                txtNumber2.Attributes["style"] = "display:block";
                                lblNumber2.Attributes["style"] = "display:block";
                                if (msDetail.OneCodeNum == "0")
                                { 
                                    lblNumber2.Text = "值   ";
                                    txtNumber2.Text = msDetail.TwoCons;
                                }
                                if (msDetail.TwoCodeNum == "4" || msDetail.TwoCodeNum == "5")
                                {
                                   
                                    lblNumber2.Text = "位数 ";
                                    txtNumber2.Text = msDetail.TwoMedian;
                                }
                            }
                            ddlTwoSeparator.Text = (msDetail.TwoSeparator != "") ? msDetail.TwoSeparator : "";
                        }
                        if (msDetail.ThreeCodeNum != "")
                        {
                            ddlCode3.SelectedValue = (msDetail.ThreeCodeNum != "") ? msDetail.ThreeCodeNum : "";
                            if (msDetail.ThreeCodeNum == "0" || msDetail.ThreeCodeNum == "4" || msDetail.ThreeCodeNum == "5")
                            {
                                txtNumber3.Attributes["style"] = "display:block";
                                lblNumber3.Attributes["style"] = "display:block";
                                if (msDetail.ThreeCodeNum == "0")
                                { 
                                    lblNumber3.Text = "值   ";
                                    txtNumber3.Text = msDetail.ThreeCons;
                                }
                                if (msDetail.ThreeCodeNum == "4" || msDetail.ThreeCodeNum == "5")
                                { 
                                    lblNumber3.Text = "位数 ";
                                    txtNumber3.Text = msDetail.ThreeMedian;
                                }
                            }
                            ddlThreeSeparator.Text = (msDetail.ThreeSeparator != "") ? msDetail.ThreeSeparator : "";
                        }
                        if (msDetail.FourCodeNum != "")
                        {
                            ddlCode4.SelectedValue = (msDetail.FourCodeNum != "") ? msDetail.FourCodeNum : "";
                            if (msDetail.FourCodeNum == "0" || msDetail.FourCodeNum == "4" || msDetail.FourCodeNum == "5")
                            {
                                txtNumber4.Attributes["style"] = "display:block";
                                lblNumber4.Attributes["style"] = "display:block";
                                if (msDetail.FourCodeNum == "0")
                                { 
                                    lblNumber4.Text = "值   ";
                                    txtNumber4.Text = msDetail.FourCons;
                                }
                                if (msDetail.FourCodeNum == "4" || msDetail.FourCodeNum == "5")
                                { 
                                    lblNumber4.Text = "位数 ";
                                    txtNumber4.Text = msDetail.FourMedian;
                                }
                            }
                            ddlFourSeparator.Text = (msDetail.FourSeparator != "") ? msDetail.FourSeparator : "";
                        }
                        if (msDetail.FiveCodeNum != "")
                        {
                            ddlCode5.SelectedValue = (msDetail.FiveCodeNum != "") ? msDetail.FiveCodeNum : "";
                            if (msDetail.FiveCodeNum == "0" || msDetail.FiveCodeNum == "4" || msDetail.FiveCodeNum == "5")
                            {
                                txtNumber5.Attributes["style"] = "display:block";
                                lblNumber5.Attributes["style"] = "display:block";
                                if (msDetail.FiveCodeNum == "0")
                                {  
                                    lblNumber5.Text = "值   ";
                                    txtNumber5.Text = msDetail.FourCons;
                                }
                                if (msDetail.FiveCodeNum == "4" || msDetail.FiveCodeNum == "5")
                                { 
                                    lblNumber5.Text = "位数 ";
                                    txtNumber5.Text = msDetail.FiveMedian;
                                }
                            }
                            ddlFiveSeparator.Text = (msDetail.FiveSeparator != "") ? msDetail.FiveSeparator : "";
                        }
                    }
                }
            }
        }
        private void DataBinde()
        {
            CtrInterface.BindDropDown(ref ddlClassType, "DOC_B1", "DOC_B1_10", "DOC_B1_20", true, "");
            this.txtCodeName.Text = "";
            this.ddlCode1.SelectedValue = "";
            this.ddlCode2.SelectedValue = "";
            this.ddlCode3.SelectedValue = "";
            this.ddlCode4.SelectedValue = "";
            this.ddlCode5.SelectedValue = "";
            ddlOneSeparator.Text = "";
            ddlTwoSeparator.Text = "";
            ddlThreeSeparator.Text = "";
            ddlFourSeparator.Text = "";
            ddlFiveSeparator.Text = "";
            txtNumber1.Text ="";
            txtNumber2.Text = "";
            txtNumber3.Text = "";
            txtNumber4.Text = "";
            txtNumber5.Text = "";
            chkDefault.Checked = false;
        }
        /// <summary>
        /// save CodeRule
        /// </summary>
        /// <param name="jsonExpr"></param>
        /// <param name="ctrlType"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DoSaveUpdate(string jsonExpr)
        {
            MsCodeRule ms = (MsCodeRule)JavaScriptConvert.DeserializeObject(jsonExpr, typeof(MsCodeRule)); 
            SystemDocBLL BLL = new SystemDocBLL();
            List<string> lstResult = BLL.DoSaveCodeRuleMes(ms);
            var newObj = new { resultSign = lstResult[0], message = lstResult[2], name = lstResult[1], resultType = "Save" };
            string serExpr = JavaScriptConvert.SerializeObject(newObj); 
            ms = null;
            BLL = null;
            return serExpr;
        }
        [global::System.Web.Services.WebMethod]
        public static string DoOperate(string codeId)
        { 
            SystemDocBLL BLL = new SystemDocBLL();
            List<string> lstResult = BLL.DoOperateCodeRule(codeId);
            var newObj = new { resultSign = lstResult[0], message = lstResult[2], name = lstResult[1], resultType = "Delete" };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            
            BLL = null;
            return serExpr;
        }

        protected void lnbCallBack_Click(object sender, EventArgs e)
        {
            DataBinde();
            ddlClassType.Enabled = true;
        }
    }
}
